 /*
 用axios封装Ajax请求返回promise类型数据
 */
import axios from 'axios' 
import qs from 'qs';

const tokenName = "vue_iview_token"
const clientPlatform = "vue_iview"
const clientVersion = "vue_iview"

'use strict'  //"use strict" 的目的是指定代码在严格条件下执行。 严格模式下你不能使用未声明的变量。
// axios interceptors 拦截器中添加headers 属性
axios.interceptors.request.use(
    config => {
        const token = localStorage.getItem(tokenName);
        if (token) {
            config.headers['Access-Token'] = token;
        }
        config.headers["CLIENT-PLATFORM"] = clientPlatform;
        config.headers["CLIENT-VERSION"] = clientVersion;
        return config
    },
    error => {
        return Promise.reject(error)//es6 报错回调
    }
)

export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'post',
            data: qs.stringify(data)
        }).then(response => {
            if (interceptorsHttpError(response) === false) {
                return;
            }
            resolve(response.data)
        }, error => { 
            reject(error)
        })
    })
}

export function get(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'get',
            data: qs.stringify(data)
        }).then(response => {
            if (interceptorsHttpError(response) === false) {
                return;
            }
            resolve(response.data)
        }, error => { 
            reject(error)
        })
    })
}

export function postBody(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'post',
            headers: { 'content-type': 'application/json' },
            data: JSON.stringify(data),
        }).then(response => {
            if (interceptorsHttpError(response) === false) {
                return;
            }
            resolve(response.data)
        }, error => { 
            reject(error)
        })
    })
}

export function upload(url, file) {
    const param = new FormData();
    param.append("file", file);
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'post',
            headers: { "Content-Type": "multipart/form-data" },
            data: param,
        }).then(response => {
            if (interceptorsHttpError(response) === false) {
                return;
            }
            resolve(response.data)
        }, error => { 
            reject(error)
        })
    })
}


//全局 导出 表格的请求方法
export function postByExport(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'post',
            headers: {'content-type': 'application/x-www-form-urlencoded;charset=UTF-8'},
            data: qs.stringify(parseData(data)),
            responseType: 'blob',
        }).then(response => {
            if (interceptorsHttpError(response) === false) {
                return;
            }
            resolve(response.data)
        }, error => {
            reject(error)

        })
    })
}

//全局 导出 表格的请求方法
export function postBodyByExport(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios({
            url: url,
            method: 'post',
            headers: {'content-type': 'application/json;charset=UTF-8'},
            data: JSON.stringify(data),
            responseType: 'blob',
        }).then(response => { 
            if (interceptorsHttpError(response) === false) {
                return;
            }
            resolve(response.data)
        }, error => {
            reject(error)

        })
    })
}

function interceptorsHttpError(res){
    if (res.data.code === '416') {
        window.location.href = "/#/login";
        localStorage.removeItem(tokenName)
        return false;
    }
    return true;
}

